home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Mac / Lib / findmodulefiles.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  2.6 KB  |  95 lines

  1. """Findmodulefiles - Find out where modules are loaded from.
  2. Run findmodulefiles() after running a program with "python -i". The
  3. resulting file list can be given to mkfrozenresources or to a
  4. (non-existent) freeze-like application.
  5.  
  6. findmodulefiles will create a file listing all modules and where they have
  7. been imported from.
  8.  
  9. findunusedbuiltins takes a list of (modules, file) and prints all builtin modules
  10. that are _not_ in the list. The list is created by opening the findmodulefiles
  11. output, reading it and eval()ing that.
  12.  
  13. mkpycresource takes a list of (module, file) and creates a resourcefile with all those
  14. modules and (optionally) a main module.
  15. """
  16.  
  17. def findmodulefiles(output=None):
  18.     """Produce a file containing a list of (modulename, filename-or-None)
  19.     tuples mapping module names to source files"""
  20.     # Immedeately grab the names
  21.     import sys
  22.     module_names = sys.modules.keys()[:]
  23.     import os
  24.     if not output:
  25.         if os.name == 'mac':
  26.             import macfs
  27.             
  28.             output, ok = macfs.StandardPutFile('Module file listing output:')
  29.             if not ok: sys.exit(0)
  30.             output = output.as_pathname()
  31.     if not output:
  32.         output = sys.stdout
  33.     elif type(output) == type(''):
  34.         output = open(output, 'w')
  35.     output.write('[\n')
  36.     for name in module_names:
  37.         try:
  38.             source = sys.modules[name].__file__
  39.         except AttributeError:
  40.             source = None
  41.         else:
  42.             source = `source`
  43.         output.write('\t(%s,\t%s),\n' % (`name`, source))
  44.     output.write(']\n')
  45.     del output
  46.     
  47. def findunusedbuiltins(list):
  48.     """Produce (on stdout) a list of unused builtin modules"""
  49.     import sys
  50.     dict = {}
  51.     for name, location in list:
  52.         if location == None:
  53.             dict[name] = 1
  54.     for name in sys.builtin_module_names:
  55.         if not dict.has_key(name):
  56.             print 'Unused builtin module:', name
  57.             
  58.  
  59. def mkpycresourcefile(list, main='', dst=None):
  60.     """Copy list-of-modules to resource file dst."""
  61.     import py_resource
  62.     import Res
  63.     import sys
  64.     
  65.     if dst == None:
  66.         import macfs
  67.         fss, ok = macfs.StandardPutFile("PYC Resource output file")
  68.         if not ok: sys.exit(0)
  69.         dst = fss.as_pathname()
  70.     if main == '':
  71.         import macfs
  72.         fss, ok = macfs.PromptGetFile("Main program:", "TEXT")
  73.         if ok:
  74.             main = fss.as_pathname()
  75.     
  76.     fsid = py_resource.create(dst)
  77.  
  78.     if main:
  79.         id, name = py_resource.frompyfile(main, '__main__', preload=1)
  80.         print '%5d\t%s\t%s'%(id, name, main)
  81.     for name, location in list:
  82.         if not location: continue
  83.         if location[-4:] == '.pyc':
  84.             # Attempt corresponding .py
  85.             location = location[:-1]
  86.         if location[-3:] != '.py':
  87.             print '*** skipping', location
  88.             continue
  89.         id, name = py_resource.frompyfile(location, name, preload=1)
  90.         print '%5d\t%s\t%s'%(id, name, location)
  91.  
  92.     Res.CloseResFile(fsid)
  93.  
  94.             
  95.